<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

Yii::import('application.extensions.HttpClient.HttpClient');
class CUserController extends Controller
{
    protected function beforeAction ($action)
    {
                $this->autoLogin();
        if (isset($_POST['SESSIONID'])) {
            if (! isset(Yii::app()->request->cookies['PHPSESSID']) ||
             $_POST['SESSIONID'] != Yii::app()->request->cookies['PHPSESSID']->value) {
                $user = Yii::app()->session["user"];
                if (empty($user)) {
                    session_destroy();
                    session_id($_POST['SESSIONID']);                     session_start();
                    if (isset(Yii::app()->request->cookies['PHPSESSID'])) {
                        $_COOKIE['PHPSESSID'] = $_POST['SESSIONID'];
                    }
                }
            }
        }
        $user = Yii::app()->session["user"];
        if (! isset($user)) {
            $this->redirect(Yii::app()->createUrl("site/login"));
        }
        return true;
    }
    
    protected function autoLogin ()
    {
                if (@$_REQUEST['client'] != "win" ||
         @$_REQUEST['account_info'] == "") {
            return;
        }
        $r = 'netdisk/index';
        if (@$_REQUEST["r"] != 'netdisk/index') {
            return;
        }
                if (@$_REQUEST["d"] == 'setting/index') {
            $_SESSION['setting/index'] = true;
        }
        
        $url = @$_REQUEST['account_info'];
        $retval = HttpClient::quickGet($url);
                $retval = @json_decode($retval,true);
        if ($retval) {
            $userName = $retval['user_name'];
            $user = User::model()->find("user_name=?", array($userName));
            if (!$user) {
                return ;
            }
            
            $userBase = new UserBase();
            $userBase->load($user->id);
            Yii::app()->session["user"] = $userBase->serialize();
                        Yii::app()->session["appId"] = 1;             $userDevice = new UserDevice();
            $device = $userDevice->find("user_id=? and user_device_type=1", $userBase->id);             if (! isset($device)) {
                $device = new UserDevice();
                $device->user_device_uuid = CUtils::genRandomString(32);
                $device->user_id = $userBase->id;
                $device->user_device_type = 1;
                $device->user_device_name = "web";
                $device->user_device_info = "web";
                $device->save();
            }
            Yii::app()->session["deviceId"] = $device->id;             $this->redirect(Yii::app()->createUrl($r));
        }
    }
}